﻿using System;
using System.Collections.Generic;
using Microsoft.SharePoint.StsAdmin;
using System.Text;
using Microsoft.SharePoint;
using System.Collections.Specialized;
using System.IO;

namespace Macaw.Moss2007.StsadmExtensionsSolution
{
    public class CreateListFromTemplateExtension : ISPStsadmCommand
    {
        public string GetHelpMessage(string command)
        {
            return "-url <url of web to create list> -stp <name of the list including .stp> [-title \"<title of the created list>\"] [-description \"<description of the created list>\"]";
        }

        public int Run(string command, StringDictionary keyValues, out string output)
        {

            command = command.ToLowerInvariant();

            switch (command)
            {
                case "createlistfromtemplate":
                    return this.CreateList(keyValues, out output);

                default:
                    throw new InvalidOperationException();
            }
        }

        private int CreateList(StringDictionary keyValues, out string output)
        {
            output = "Something went wrong!";

            if (!keyValues.ContainsKey("url"))
            {
                output = GetHelpMessage("") + "\r\nThe 'url' parameter was not specified.";
                return -1;
            }

            String url = keyValues["url"];

            if (!keyValues.ContainsKey("stp"))
            {
                output = GetHelpMessage("") + "\r\nThe 'stp' parameter was not specified.";
                return -1;
            }

            String stp = keyValues["stp"];

            try
            {
                using (SPSite site = new SPSite(url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPListTemplateCollection listtemplates = site.GetCustomListTemplates(web);
                        foreach (SPListTemplate listtemplate in listtemplates)
                        {
                            if (listtemplate.InternalName.Equals(stp, StringComparison.InvariantCultureIgnoreCase))
                            {
                                string name = listtemplate.Name;
                                string title = listtemplate.Name;
                                string description = listtemplate.Description;

                                if (keyValues.ContainsKey("title"))
                                {
                                    title = keyValues["title"];
                                }

                                if (keyValues.ContainsKey("description"))
                                {
                                    description = keyValues["description"];
                                }
                                Guid g = web.Lists.Add(title, description,listtemplate);
                                SPList list = web.Lists[g];
                                output = string.Format("List succesfully created with the name '{0}'\r\n", list.Title);
                                return 0;
                            }
                        }


                        output = string.Format("Error! Listtemplate with name \"{0}\" not found", new object[]{stp});
                        return -1;
                    }

                }
            }

            catch (Exception ex)
            {
                output = ex.Message;
            }
            return -1;
        }

    }
}
